/**
 * 
 */
$().ready(
		function() {
			var getSubjectAll = '/OnlineExam/teacher/getSubjectAll';
			var getTestTypeAll = '/OnlineExam/teacher/getTestTypeAll';

			$('#addTestForm .answerTotal input:checkbox').hide();
			$('#editTestForm .answerTotal input:checkbox').hide();
			
			$.getJSON(getSubjectAll, function(data) {
				if (data.success) {
					var tempSubjectHtml = '';
					data.subjectlist.map(function(item, index) {
						tempSubjectHtml += '<option value="' + item.subId
								+ '">' + item.name + '</option>';
					});
				}
				$('#subId').html(tempSubjectHtml);
				$('.editSubId').html(tempSubjectHtml);
			})
			$.getJSON(getTestTypeAll, function(data) {
				if (data.success) {
					var tempTestTypeHtml = '';
					data.examTestTypelist.map(function(item, index) {
						tempTestTypeHtml += '<option value="' + item.id
								+ '" score="' + item.testTypeScore + '">'
								+ item.testType + '</option>';
					});
				}
				$('#testType').html(tempTestTypeHtml);
				$('.edittestTypeEdit').html(tempTestTypeHtml);
			})
		})
		
$('.btn-info').click(function() {
	testId = $(this).attr('ModelId');
	editUrl = "/OnlineExam/teacher/editTest/"+testId;
	$.ajax({
		url:"/OnlineExam/teacher/getByIdTest/"+testId,
		type:"GET",
		dataType:"json",
		success:function(data) {
			if(data.success){
				$('#editTextArea').val(data.testmodelByOneId.content);
				$('#editChooseA').val(data.testmodelByOneId.chooseA);
				$('#editChooseB').val(data.testmodelByOneId.chooseB);
				$('#editChooseC').val(data.testmodelByOneId.chooseC);
				$('#editChooseD').val(data.testmodelByOneId.chooseD);
			}
		}
	})
})

$('.btn-warning').click(function() {
	subId = $(this).attr('ModelId');
	$.ajax({
		url:"/OnlineExam/teacher/getByIdTest/"+subId,
		type:"GET",
		dataType:"json",
		success:function(data) {
			if(data.success){
				$('#TestSubjectDetails').html(data.testmodelByOneId.subjectId.name);
				$('#TestDetails').html(data.testmodelByOneId.content);
				$('#ChooseADetails').html(data.testmodelByOneId.chooseA);
				$('#ChooseBDetails').html(data.testmodelByOneId.chooseB);
				$('#ChooseCDetails').html(data.testmodelByOneId.chooseC);
				$('#ChooseDDetails').html(data.testmodelByOneId.chooseD);
				$('#AnswerDetails').html(data.testmodelByOneId.answer);
			}
		}
	})
})		

$('#addTestForm select[name="testType"]').change(
		function() {
			$('#addTestForm .testTypeScore')
					.text(
							'本题每题'
									+ $(this).find('option:checked').attr(
											'score') + '分')
			var testType = $(this).val();
			if (testType == 1) { // 单选
				$('#addTestForm .answerTotal input:radio').show();
				$('#addTestForm .answerTotal input:checkbox').hide();
			} else if (testType == 2) { // 多选
				$('#addTestForm .answerTotal input:checkbox').show();
				$('#addTestForm .answerTotal input:radio').hide();
			} else { // 其他
				alert('无些试题类型');
			}
		});
$('#editTestForm select[name="testType"]').change(
		function() {
			$('.editTestForm .testTypeScore')
					.text(
							'本题每题'
									+ $(this).find('option:checked').attr(
											'score') + '分')
			var testType = $(this).val();

			if (testType == 1) { // 单选
				$('#editTestForm .answerTotal input:radio').show();
				$('#editTestForm .answerTotal input:checkbox').hide();
			} else if (testType == 2) { // 多选
				$('#editTestForm .answerTotal input:checkbox').show();
				$('#editTestForm .answerTotal input:radio').hide();
			} else { // 其他
				alert('无些试题类型');
			}
		});
// 保存并添加
$('#addTestSaveBtn')
		.click(
				function() {
					if ($('#addTestForm').valid()) {
						var subId = $('#addTestForm select[name="subId"]')
								.val();
						var testType = $('#addTestForm select[name="testType"]')
								.val();
						var content = $('#addTestForm textarea[name="content"]')
								.val();
						var chooseA = $('#addTestForm input[name="chooseA"]')
								.val();
						var chooseB = $('#addTestForm input[name="chooseB"]')
								.val();
						var chooseC = $('#addTestForm input[name="chooseC"]')
								.val();
						var chooseD = $('#addTestForm input[name="chooseD"]')
								.val();
						var answerString = '';
						if (testType == 1) {
							answerString += $(
									'#addTestForm input[name="answer"]:radio:checked')
									.val();
						} else if (testType == 2) {
							var array = $('#addTestForm input[name="answer"]:checkbox:checked');
							$.each(array, function(index, item) {
								answerString += (index == 0 ? $(item).val()
										: (',' + $(item).val()));
							});
						} else {
							alert('无些试题类型!');
						}
						$
								.ajax({
									type : 'POST',
									url : '/OnlineExam/teacher/saveTest',
									data : {
										subId : subId,
										examTestTypeId : testType,
										content : content,
										chooseA : chooseA,
										chooseB : chooseB,
										chooseC : chooseC,
										chooseD : chooseD,
										answer : answerString
									},
									dataType : "json",
									success : function(data) {
										// 是否成功
										if (data.success) {
											alert("添加成功！");
											window.location.reload();
										} else {
											// 不成功显示错误信息
											var messageMap = data.messageMap;
											for ( var key in messageMap) {
												var value = messageMap[key];
												$(
														'#addTestForm input[name="'
																+ key + '"]')
														.after(
																'<label class="jsr303-message">')
											}
											alert('添加失败！-'
													+ messageMap['message']);
										}

									}
								});
					}
				});

// 修改并保存
$('#editTestSaveBtn')
		.click(
				function() {
					if ($('.editTestForm').valid()) {
						var subId = $('#editTestForm select[name="subId"]')
								.val();
						var testType = $(
								'#editTestForm select[name="testType"]').val();
						var content = $(
								'#editTestForm textarea[name="content"]').val();
						var chooseA = $('#editTestForm input[name="chooseA"]')
								.val();
						var chooseB = $('#editTestForm input[name="chooseB"]')
								.val();
						var chooseC = $('#editTestForm input[name="chooseC"]')
								.val();
						var chooseD = $('#editTestForm input[name="chooseD"]')
								.val();
						var answerString = '';
						if (testType == 1) {
							answerString += $(
									'#editTestForm input[name="answer"]:radio:checked')
									.val();
						} else if (testType == 2) {
							var array = $('#editTestForm input[name="answer"]:checkbox:checked');
							$.each(array, function(index, item) {
								answerString += (index == 0 ? $(item).val()
										: (',' + $(item).val()));
							});
						} else {
							alert('无些试题类型!');
						}
						$
								.ajax({
									type : 'POST',
									url : editUrl,
									data : {
										id:testId,
										subId : subId,
										examTestTypeId : testType,
										content : content,
										chooseA : chooseA,
										chooseB : chooseB,
										chooseC : chooseC,
										chooseD : chooseD,
										answer : answerString
									},
									dataType : "json",
									success : function(data) {
										// 是否成功
										if (data.success) {
											alert("修改成功！");
											window.location.reload();
										} else {
											// 不成功显示错误信息
											var messageMap = data.messageMap;
											for ( var key in messageMap) {
												var value = messageMap[key];
												$(
														'#editTestForm input[name="'
																+ key + '"]')
														.after(
																'<label class="jsr303-message">')
											}
											alert('修改失败！-'
													+ messageMap['message']);
										}

									}
								});
					}

				});

